<%-- 
    Document   : schedule
    Created on : Sep 23, 2011, 10:49:28 AM
    Author     : Yudi Retanto
--%>

<%@page import="com.moedik.classes.BusService"%>
<%@page import="com.moedik.model.BusModel"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:include page="/header.jsp"></jsp:include>
<h1>
    Pilih Jadwal Keberangkatan
</h1>
<table>
    <tr>
        <td colspan="6" style="text-transform: capitalize">
            ${now}
        </td>
    </tr>
    <tr>
        <th class="td-no">No</th>
        <th>Kota Tujuan</th>
        <th>Pool Tujuan</th>
        <th>Kelas</th>
        <th>Jam</th>
        <th>Harga</th>
        <th>Action</th>
    </tr>
    <c:forEach items="${schedules}" var="schedule">
        <c:set var="ordinal" value="${ordinal + 1}" />
        <tr id="record-${schedule.id}">
            <td class="td-no">${ordinal}</td>
            <td>${schedule.destinationCity}</td>
            <td>${schedule.destinationPool}</td>
            <td>${schedule.className}</td>
            <td>${schedule.hour}</td>
            <td>${schedule.fare}</td>
            <c:set var="service_id" value="${schedule.id}" />
            <c:set var="date" value="${now.toMillis()}" />
            <%
                BusModel buseModel = new BusModel();
                int service_id = (Integer) pageContext.getAttribute("service_id");
                long date = (Long) pageContext.getAttribute("date");
                
                if (buseModel.available(service_id, date)) {
            %>
            <td><a href="./buses?action=confirm_book&id=${schedule.id}&date=${now.toMillis()}">Book</a></td>
            <% } else {%>
            <td>SOLD OUT</td>
            <% }%>
        </tr>
    </c:forEach>
</table>
<div style="margin-top:20px;">
    <c:if test="${!now.isToday()}">
        <a href="./buses?action=view&pool=${pool}&date=${now.getYesterday()}">
            <button>Prev Day</button>
        </a>
    </c:if>
    <c:if test="${!now.isAMonthAfter()}">
        <a href="./buses?action=view&pool=${pool}&date=${now.getTomorrow()}">
            <button>Next Day</button>
        </a>
    </c:if>
</div>
<div style="margin-top:20px;">
    <script type="text/javascript">
        $(function() {
            $( "#date" ).datepicker({ dateFormat: "yy/mm/dd", minDate: 0, maxDate: "+1M" });
        });
        function filter(){
            date=$("#date").val();
            if(date==""){
                alert("Tolong masukkan tanggal keberangkatan");
            } else {
                date = new Date(date);
                date = date.getTime();
                window.location = "buses?action=view&pool=${pool}&date="+date;
            }
            return false;
        }
    </script>
    <form onsubmit="return filter()">
    <p>Go To Date <input type="text" name="date" id="date" /> <input type="submit" value="submit"/></p>
    </form>
</div>
<jsp:include page="/footer.jsp"></jsp:include>
